#include "stdafx.hpp"
#include "ColligereSearchFreeDbPanel.hpp"
#include "ColligereDatabaseInterfaceBase.hpp"

IMPLEMENT_DYNAMIC_CLASS ( ColligereSearchFreeDbPanel, wxPanel );

wxDECLARE_EVENT ( COLLIGERE_CMD_VOLGETTER_THREAD_COMPLETED, wxCommandEvent );

ColligereSearchFreeDbPanel::ColligereSearchFreeDbPanel()
{
	Bind ( COLLIGERE_CMD_VOLGETTER_THREAD_COMPLETED,
		&ColligereSearchFreeDbPanel::OnThreadCompleted );

	Bind ( wxEVT_CLOSE_WINDOW, 
		&ColligereSearchFreeDbPanel::OnClose, GetId() );

	Bind ( wxEVT_COMMAND_COMBOBOX_SELECTED,
		&ColligereSearchFreeDbPanel::OnDeviceSelected,
		XRCID ( "ID_ComboBox_Devices" ) );

	Bind ( wxEVT_COMMAND_BUTTON_CLICKED,
		&ColligereSearchFreeDbPanel::OnButtonSearch,
		XRCID ( "ID_Button_SearchFreeDb" ) );

	Bind ( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED,
		&ColligereSearchFreeDbPanel::OnSearchResultDClick,
		XRCID ( "ID_ListBox_SearchResults" ) );
}

void ColligereSearchFreeDbPanel::Initialize()
{
	if ( CreateThread ( wxTHREAD_JOINABLE ) != wxTHREAD_NO_ERROR ) return;
    if ( GetThread()->Run() != wxTHREAD_NO_ERROR ) return;
}

wxThread::ExitCode ColligereSearchFreeDbPanel::Entry()
{
	mMountedDrives = col::CddbDiscId::GetMountedDrives ( false );
	wxQueueEvent ( this, new wxCommandEvent ( COLLIGERE_CMD_VOLGETTER_THREAD_COMPLETED ) );
	return ( wxThread::ExitCode ) 0;
}

void ColligereSearchFreeDbPanel::OnDeviceSelected ( wxCommandEvent &rEvt )
{
	XRCCTRL ( *this, "ID_Button_SearchFreeDb", 
		wxButton )->Enable ( !rEvt.GetString().empty() );
}

void ColligereSearchFreeDbPanel::OnButtonSearch ( wxCommandEvent & )
{
	XRCCTRL ( *this, "ID_Button_SearchFreeDb", wxButton )->Disable();
	XRCCTRL ( *this, "ID_ListBox_SearchResults", wxListBox )->Clear();
	mCurSearchResults.clear();

	const wxString device ( XRCCTRL ( *this, "ID_ComboBox_Devices", 
		wxComboBox )->GetValue() );

	wxASSERT ( !device.empty() );

	mFreeDbSearcher.SetDevice ( device );
	foreach ( const col::SearchFreeDb::SearchResult &r, mFreeDbSearcher.Search() )
	{
		// Note to self: Don't use wxLB_SORT in ID_ListBox_SearchResults.
		const int idx ( XRCCTRL ( *this, "ID_ListBox_SearchResults", 
			wxListBox )->Append ( r.GetTitle() ) );
		mCurSearchResults[idx] = r;
	}
	XRCCTRL ( *this, "ID_Button_SearchFreeDb", wxButton )->Enable();
}

void ColligereSearchFreeDbPanel::OnSearchResultDClick ( wxCommandEvent &rEvt )
{
	using namespace col;

	wxASSERT ( !mCurSearchResults.empty() );

	const SearchFreeDb::SearchResult r ( mCurSearchResults[rEvt.GetSelection()] );
	wxASSERT ( !r.GetDiscId().empty() && !r.GetCategory().empty() && !r.GetTitle().empty() );

	DatabaseInterfaceBase::GetActive()->GetLayout()->ClearInputControls();

	const Artist artist ( mFreeDbSearcher.DownloadResult(r) );

	DatabaseInterfaceBase::GetActive()->GetLayout()->PassItemToInputControls ( artist );
	wxASSERT ( artist.GetChildCount() != 0 );
	
	if ( artist.GetChildCount() != 0 )
	{
		DatabaseInterfaceBase::GetActive()->GetLayout()->PassItemToInputControls 
			( *artist.GetFirstChild() );
	}
}

void ColligereSearchFreeDbPanel::OnClose ( wxCloseEvent & )
{
	if ( GetThread() && GetThread()->IsRunning() ) GetThread()->Wait();
	Destroy();
}

void ColligereSearchFreeDbPanel::OnThreadCompleted ( wxCommandEvent & )
{
	foreach ( const std::string &d, mMountedDrives )
	{
		XRCCTRL ( *this, "ID_ComboBox_Devices", wxComboBox )->Append(d);
	}
}